package com.ruoyi.mes.systembasic.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mes.systembasic.domain.SmesEmployeePost;
import com.ruoyi.mes.systembasic.mapper.SmesEmployeePostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.systembasic.mapper.SmesEmployeeMapper;
import com.ruoyi.mes.systembasic.domain.SmesEmployee;
import com.ruoyi.mes.systembasic.service.ISmesEmployeeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 员工管理Service业务层处理
 * 
 * @author yanqin
 * @date 2020-11-24
 */
@Service
public class SmesEmployeeServiceImpl implements ISmesEmployeeService 
{
    @Autowired
    private SmesEmployeeMapper smesEmployeeMapper;

    @Autowired
    private SmesEmployeePostMapper smesEmployeePostMapper;

    /**
     * 查询员工管理
     * 
     * @param employeeNo 员工管理ID
     * @return 员工管理
     */
    @Override
    public SmesEmployee selectSmesEmployeeById(String employeeNo)
    {
        return smesEmployeeMapper.selectSmesEmployeeById(employeeNo);
    }

    /**
     * 查询员工管理列表
     * 
     * @param smesEmployee 员工管理
     * @return 员工管理
     */
    @Override
    public List<SmesEmployee> selectSmesEmployeeList(SmesEmployee smesEmployee)
    {
        return smesEmployeeMapper.selectSmesEmployeeList(smesEmployee);
    }

    /**
     * 新增员工管理
     * 
     * @param smesEmployee 员工管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSmesEmployee(SmesEmployee smesEmployee)
    {
        smesEmployee.setCreateTime(DateUtils.getNowDate());
        int rows= smesEmployeeMapper.insertSmesEmployee(smesEmployee);
        insertEmployeePost(smesEmployee);
        return rows;
    }

    /**
     * 新增用户岗位信息
     *
     * @param employee 用户对象
     */
    public void insertEmployeePost(SmesEmployee employee)
    {
        Long[] posts = employee.getPostIds();
        if (StringUtils.isNotNull(posts))
        {
            // 新增用户与岗位管理
            List<SmesEmployeePost> list = new ArrayList<>();
            for (Long postId : posts)
            {
                SmesEmployeePost ep = new SmesEmployeePost();
                ep.setEmployeeId(employee.getRowId());
                ep.setPostId(postId);
                list.add(ep);
            }
            if (list.size() > 0)
            {
                smesEmployeePostMapper.batchEmployeePost(list);
            }
        }
    }
    /**
     * 修改员工管理
     * 
     * @param smesEmployee 员工管理
     * @return 结果
     */
    @Override
    public int updateSmesEmployee(SmesEmployee smesEmployee)
    {
        smesEmployee.setUpdateTime(DateUtils.getNowDate());

        int rows= smesEmployeeMapper.updateSmesEmployee(smesEmployee);
        // 删除用户与岗位关联
        smesEmployeePostMapper.deleteSmesEmployeePostById(smesEmployee.getRowId());
        insertEmployeePost(smesEmployee);
        return rows;
    }

    /**
     * 批量删除员工管理
     * 
     * @param employeeNos 需要删除的员工管理ID
     * @return 结果
     */
    @Override
    public int deleteSmesEmployeeByIds(String[] employeeNos)
    {
        return smesEmployeeMapper.deleteSmesEmployeeByIds(employeeNos);
    }


    /**
     * 删除员工管理信息
     * 
     * @param employeeNo 员工管理ID
     * @return 结果
     */
    @Override
    public int deleteSmesEmployeeById(String employeeNo)
    {
        return smesEmployeeMapper.deleteSmesEmployeeById(employeeNo);
    }
    /**
     * 查询所有员工
     *
     * @return 员工列表
     */
    @Override
    public List<SmesEmployee> selectEmployeeAll()
    {
        return smesEmployeeMapper.selectEmployeeAll();
    }
}
